package com.east.sng.controller;

import com.east.sng.entity.param.CourseTypeParam;
import com.east.sng.entity.result.CourseTypeFind;
import com.east.sng.service.CourseTypeService;
import com.east.sng.utils.ValidationUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @author east
 * @date: 2020/12/28
 * @Description:
 **/
@Controller
@RequestMapping("/courseType")
public class CourseTypeController {
    @Autowired
    private CourseTypeService courseTypeService;

    /**
     * 课程分类信息页面跳转
     * @param courseTypeName
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequiresPermissions("courseType:list")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(String courseTypeName, @RequestParam(name="flag",defaultValue="0")Integer flag,
                       @RequestParam(name="pn",defaultValue="1")Integer pageNum,
                       @RequestParam(name = "pSize", defaultValue = "5") Integer pageSize, Model model){
        model.addAttribute("page",PageHelper.startPage(pageNum,pageSize));
        model.addAttribute("courseTypes",this.courseTypeService.findCourseTypes(courseTypeName,flag));
        return "courseType/list";
    }

    /**
     * 课程类型录入页面跳转
     * @return
     */
    @RequiresRoles(value = {"0","1"},logical = Logical.OR)
    @RequiresPermissions("courseType:add")
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String toAddCourseType(){
        return "courseType/add";
    }

    /**
     * 课程类型录入
     * @param courseType
     * @param br
     * @return
     */
    @RequiresRoles(value = {"0","1"},logical = Logical.OR)
    @RequiresPermissions("courseType:add")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String addCourseType(@Valid CourseTypeParam courseType, BindingResult br){
        ValidationUtil.validateData(br);
        this.courseTypeService.addCourseType(courseType);
        return "success";
    }

    /**
     * 课程页面修改页面跳转
     * @param courseTypeId
     * @param model
     * @return
     */
    @RequiresRoles(value = {"0","1"},logical = Logical.OR)
    @RequiresPermissions(value = {"courseType:list","courseType:add"},logical = Logical.AND)
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public String toEditCourseType(Integer courseTypeId,Model model){
        model.addAttribute("courseType",this.courseTypeService.findCourseType(courseTypeId));
        return "courseType/edit";
    }

    /**
     * 课程类型修改
     * @param courseType
     * @param br
     * @return
     */
    @RequiresRoles(value = {"0","1"},logical = Logical.OR)
    @RequiresPermissions(value = {"courseType:list","courseType:add"},logical = Logical.AND)
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String editCourseType(@Valid CourseTypeParam courseType, BindingResult br){
        ValidationUtil.validateData(br);
        this.courseTypeService.editCourseType(courseType);
        return "success";
    }

    /**
     * 课程类型删除
     * @param id
     * @return
     */
    @RequiresRoles(value = {"0","1"},logical = Logical.OR)
    @RequiresPermissions(value = {"courseType:list","courseType:add"},logical = Logical.AND)
    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @ResponseBody
    public String removeCourseType(Integer id){
        return this.courseTypeService.removeCourseType(id)?"success":"fail";
    }

    /**
     * 课程类型恢复
     * @param id
     * @return
     */
    @RequiresRoles("0")
    @RequiresPermissions(value = {"courseType:list","courseType:add"},logical = Logical.AND)
    @RequestMapping(value = "/recover",method = RequestMethod.POST)
    @ResponseBody
    public String recoverCourseType(Integer id){
        return this.courseTypeService.recoverCourseType(id)?"success":"fail";
    }

    /**
     * 课程类型批量删除
     * @param ids
     * @return
     */
    @RequiresRoles(value = {"0","1"},logical = Logical.OR)
    @RequiresPermissions(value = {"courseType:list","courseType:add"},logical = Logical.AND)
    @RequestMapping(value = "/removes",method = RequestMethod.POST)
    @ResponseBody
    public String removeCourseTypes(Integer[] ids){
        return this.courseTypeService.batchRemoveCourseTypes(Arrays.asList(ids))?"success":"fail";
    }

    /**
     * 课程类型批量恢复
     * @param ids
     * @return
     */
    @RequiresRoles("0")
    @RequiresPermissions(value = {"courseType:list","courseType:add"},logical = Logical.AND)
    @RequestMapping(value = "/recovers",method = RequestMethod.POST)
    @ResponseBody
    public String recoverCourseTypes(Integer[] ids){
        return this.courseTypeService.batchRecoverCourseTypes(Arrays.asList(ids))?"success":"fail";
    }

}
